package ntd.util.frame.action.function.video;

import cn.hutool.json.JSONUtil;
import ntd.action.ActionAbs;
import ntd.action.bo.ActionInfo;
import ntd.action.bo.ActionOutBo;
import ntd.util.frame.action.function.video.bo.ActionVideoInBo;
import ntd.util.frame.action.function.video.tool.ToolDaHua;
import ntd.util.frame.haikang.remote.HaiKangConfig;
import ntd.util.frame.haikang.remote.RemoteHaiKang;
import ntd.util.modular.function.entity.ChannelBusiness;
import ntd.util.modular.function.entity.Platform;
import ntd.util.modular.function.util.UtilChannelBusiness;
import ntd.util.modular.function.util.UtilPlatform;

public class ActionVideoGetUrl extends ActionAbs {

    @Override
    public ActionInfo info() {
        return new ActionInfo("视频取流链接获取", "action-video-get-url", new ActionVideoGetUrl());
    }

    /**
     * /action/action-video-get-url/91d08654e7c84669b6d25b2694e9fa30
     * {
     *     "cameraIndexCode": "1006567$1$0$0",
     *     "streamUrlProtocol": "PROTOCOL_RTSP"
     * }
     * @return
     */
    @Override
    public ActionOutBo handle() {
        

        String dataCode = actionInBo.getDataCode();

        ActionVideoInBo actionVideoInBo = JSONUtil.toBean(actionInBo.getDataStr(), ActionVideoInBo.class);

        ChannelBusiness channelBusiness = UtilChannelBusiness.getById(dataCode);

        return new ActionOutBo(getUrl(channelBusiness, actionVideoInBo));
    }

    private static String getUrl(ChannelBusiness channelBusiness, ActionVideoInBo actionVideoInBo) {
        switch (channelBusiness.getEnumPlatformType()) {
            case HAI_KANG: {
                return RemoteHaiKang.me(new HaiKangConfig()
                                .setAppKey(channelBusiness.getAppKey())
                                .setAppSecret(channelBusiness.getAppSecret())
                                .setHost(channelBusiness.getEndpoint()))
                        .getStreamUrlByCameraIndexCode(actionVideoInBo.getCameraIndexCode(), actionVideoInBo.getStreamUrlProtocol().getProtocolName());
            }
            case DA_HUA: {
                Platform platform = UtilPlatform.getById(channelBusiness.getPlatformId());
                return ToolDaHua.me(channelBusiness.getEndpoint(), platform.getAccount(), platform.getPassword(),
                                channelBusiness.getAppKey(), channelBusiness.getAppSecret()).getUrl(actionVideoInBo);
            }
        }
        return "";
    }

}
